package com.shf.controller;

import com.shf.domain.ResponseResult;
import com.shf.domain.entity.Collect;
import com.shf.service.ArticleService;
import com.shf.service.CollectService;
import com.shf.util.SecurityUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author 叶枫
 * @date 2024/5/13.
 */

@RestController
@RequestMapping("/collect")
public class CollectController {

    @Resource
    private CollectService collectService;
    @Resource
    private ArticleService articleService;

    @PostMapping("/add/{id}")
    public ResponseResult add(@PathVariable("id") Long articleId) {
        Long userId = SecurityUtils.getUserId();


        if (articleService.getById(articleId) != null && userId != null){
            Collect collect = new Collect();
            collect.setArticleId(articleId);
            collect.setUserId(userId);
            collect.setId(null);
            collectService.save(collect);
            return ResponseResult.okResult(200,"收藏成功");
        }
        return ResponseResult.okResult(500);
    }


    @GetMapping("/list/{pageNum}/{pageSize}")
    public ResponseResult getUserCollect(
            @RequestParam(value = "articleTitle", required = false) String articleTitle,
            @PathVariable("pageNum") Integer currentPage, @PathVariable("pageSize") Integer pageSize) {

        return collectService.getUserCollect(articleTitle, currentPage, pageSize);
    }
}
